package com.store.app.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.MemberOpenCardDetailAdapter;
import com.store.app.bean.OpenCardDetailBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员开卡详情页面
 */
public class MembersOpenCardDetailActivity extends BaseActivity implements IOnHttpListener {
    private AutoListView mListView;
    private NetRequest netRequest;
    private MemberOpenCardDetailAdapter memberOpenCardDetailAdapter;
    private List<OpenCardDetailBean> openCardDetailBeanList;
    private int pageNum;
    private int pageSize;
    private ImageView public_ll_return;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members_open_card_detail);
        ExitApplication.getInstance().addActivity(this);
        initView();
        openCardDetailBeanList = new ArrayList<>();
        pageNum = 1;
        pageSize = 15;
        netRequest = new NetRequest(this);
        netRequest.requestOpenCard(1, pageNum+"",pageSize+"",MainActivity.member_id);
//        netRequest.requestRegistDetail(1, pageNum+"",pageSize+"","stores", MainActivity.member_id);
//        netRequest.requesSendCouponDetail(2,"1","10",MainActivity.member_id);
        mListView = (AutoListView) findViewById(R.id.lv_members);
        memberOpenCardDetailAdapter = new MemberOpenCardDetailAdapter(openCardDetailBeanList, this);
        mListView.setAdapter(memberOpenCardDetailAdapter);
        mListView.setOnRefreshListener(new AutoListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum = 1;
                if (!TextUtils.isEmpty(MainActivity.member_id)) {
                    netRequest.requestOpenCard(2, pageNum + "", pageSize + "", MainActivity.member_id);
                }
            }
        });

        mListView.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                if (!TextUtils.isEmpty(MainActivity.member_id)) {
                    netRequest.requestOpenCard(1, pageNum + "", pageSize + "", MainActivity.member_id);
                }
            }
        });
        mListView.onRefresh();
    }

    @Override
    public void onAfinalFail() {
        mListView.onRefreshComplete();
        mListView.onLoadComplete();
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        //加载更多
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<OpenCardDetailBean> openCardDetailBeans = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    OpenCardDetailBean openCardDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), OpenCardDetailBean.class);
                    openCardDetailBeans.add(openCardDetailBean);
                }
                int page_no = data.getJSONObject("page").getInt("page_no");
                mListView.onLoadComplete();
                if (page_no == pageNum) {
                    mListView.setResultSize(openCardDetailBeans.size());
                    openCardDetailBeanList.addAll(openCardDetailBeans);
                    memberOpenCardDetailAdapter.notifyDataSetChanged();
                    pageNum += 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 2) {
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<OpenCardDetailBean> openCardDetailBeans = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    OpenCardDetailBean openCardDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), OpenCardDetailBean.class);
                    openCardDetailBeans.add(openCardDetailBean);
                }
                mListView.onRefreshComplete();
                mListView.setResultSize(openCardDetailBeans.size());
                openCardDetailBeanList.clear();
                openCardDetailBeanList.addAll(openCardDetailBeans);
                memberOpenCardDetailAdapter.notifyDataSetChanged();
                pageNum += 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        mListView.onRefreshComplete();
        mListView.onLoadComplete();
        ToastUtils.showTextToast(MembersOpenCardDetailActivity.this,msg);
    }
    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("详情");
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
